<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GENERATOR" CONTENT="Microsoft FrontPage 4.0">
   <TITLE>Sampling</TITLE>
   <link rel="stylesheet" type="text/css" href="../../avisynth.css">
</HEAD>
<BODY>
<h1>Sampling</h1>
<h2>1. Color format conversions and the Chroma Upsampling Error</h2>
<p>The following figures show errors, which are examples of <b>the Chroma Upsampling
Error</b>, called this way because the video is upsampled incorrectly (interlaced YV12 upsampled as progressive or vice versa). As a result, you will often see gaps on the top and bottom of colored objects and "ghost" lines floating above or below the objects.</p>
<table border="1" width="100%">
  <tr>
    <td width="100%" align="center"><img border="0" src="../pictures/advancedtopics/badchroma.jpg" width="728" height="400"></td>
  </tr>
  <tr>
    <td width="100%" align="center"> figure 1a: example of interlaced source (YV12) being upsampled as progressive video (YUY2)
      (from http://zenaria.com/gfx/)</td>
  </tr>
</table>
<table border="1" width="100%">
  <tr>
    <td width="100%" align="center"><img border="0" src="../pictures/advancedtopics/goodchroma.jpg" width="728" height="400"></td>
  </tr>
  <tr>
    <td width="100%" align="center"> figure 1b: the same image with correct chroma upsampling
      (from http://zenaria.com/gfx/)</td>
  </tr>
</table>
<table border="1" width="100%">
  <tr>
    <td width="100%" align="center"><img border="0" src="../pictures/advancedtopics/badchroma2.jpg" width="720" height="400"></td>
  </tr>
  <tr>
    <td width="100%" align="center"> figure 2a: example of progressive source (YV12) being upsampled as interlaced video (YUY2)</td>
  </tr>
</table>
<table border="1" width="100%">
  <tr>
    <td width="100%" align="center"><img border="0" src="../pictures/advancedtopics/goodchroma2.jpg" width="720" height="400"></td>
  </tr>
  <tr>
    <td width="100%" align="center"> figure 2b: the same image with correct chroma upsampling</td>
  </tr>
</table>
<p>In this section, it will be shown what causes it, and how to fix it. Where fixing mean making it less visible, because it's not possible to correct it completely.</p>
<p>References:<br>
[<a href="http://www.hometheaterhifi.com/volume_8_2/dvd-benchmark-special-report-chroma-bug-4-2001.html">The
Chroma Upsampling Error</a>]<br>
[<a href="http://members.aol.com/ajaynejr/vidbug2.htm">The Chroma Upsampling
Error - Television and Video Advice</a>]</p>
<h3>1.1 Chroma Upsampling Error (or CUE)</h3>
<p>As previously stated, the Chroma Upsampling Error occurs when you convert from (trully) interlaced YV12 to mostly any other format and the converter thinks the video is progressive. Or, the other way around, if material is progressive (or interlaced encoded as progressive), and upsampled as interlaced. This is however not as bad as the other way around.</p>
<p>When VDub previews your video, it will need to convert it to RGB. Since
AviSynth delivers YV12, it asks the codec (for example XviD or DivX) to convert YV12 to RGB. The codec however ALWAYS upsamples progressively. Hence you will get artifacts in VDub preview on interlaced YV12 material. This is however not present in the YV12 video (or in the resulting encoding). To confirm this, let
AviSynth do the conversion by adding ConvertToRGB(interlaced=true) at the end of your script.</p>
<h3>1.2 Correcting video having the Chroma Upsampling Error</h3>
<p>You will have to blur the chroma in some way (leaving the luma intact).</p>
<p>For example (using tomsmocomp.dll):</p>
<pre>AviSource(...)
MergeChroma(TomsMoComp(-1,5,0))</pre>
<h2>2. Theoretical Aspects</h2>
<p>In this section, the chroma placement will be explained, how this is related to subsampling (RGB -> YUY2 -> YV12) and how the upsampling is done in
AviSynth.</p>
<p>It should also explain in detail why the CUE occurs. To summarize the latter, the problem is that there is a difference between YV12 progressive and YV12 interlaced, because the chroma is shared vertically between neighboring pixels.<br>
<br>
See also <a href="http://forum.doom9.org/showthread.php?s=&amp;threadid=52151&amp;highlight=upsampling"> http://forum.doom9.org/showthread.php?s=&amp;threadid=52151&amp;highlight=upsampling</a>.</p>
<h3>2.1 The color formats: RGB, YUY2 and YV12</h3>
<p>In order to be able to understand how YV12 &lt;-> YUY2 sampling works and why it matters whether your source is interlaced or progressive, the YV12/YUY2 color formats will be discussed first. It's not important here how they are stored in your memory. Information about that can be found here:
<font color="#FF0000">ColorSpaces</font>.</p>
<h4>YUV 4:4:4 color format</h4>
<p>The term 4:4:4 denotes that for every four samples of the luminance (Y), there are four samples each of U and V. Thus each pixel has a luminance value (Y), a U value (blue difference sample or Cb) and a V value (red difference sample or Cr). Note, "C" is just a chroma sample (UV-sample).</p>
<p>The layout of a 4:4:4 encoded image looks as follows</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%"> YC YC YC YC</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%"> YC YC YC YC</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%"> YC YC YC YC</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%"> YC YC YC YC</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<h4>YUY2 color format</h4>
<p>YUY2 (or YUYV) is a 4:2:2 format. The term 4:2:2 denotes that for every four samples of the luminance (Y), there are two samples each of U and V, giving less chrominance (color) bandwidth in relation to luminance. So for each pixel, it is horizontally sharing UV (chroma) with a neighboring pixel.&nbsp;</p>
<p>The layout of a 4:2:2 encoded image looks as follows</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%"> YC Y YC Y</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%"> YC Y YC Y</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%"> YC Y YC Y</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%"> YC Y YC Y</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<h4>YV12 color format</h4>
<p>For the YV12 color format, there's a difference between progressive and interlaced. The cause is that chroma values are also shared vertically between two neighboring lines.</p>
<p>YV12 is a 4:2:0 format. The term 4:2:0 denotes that for every four samples (two horizontal and two vertical) of the luminance (Y), there is one sample each of U and V, giving less chrominance (color) bandwidth in relation to luminance.</p>
<p><b>YV12 progressive</b></p>
<p>For each pixel, it is horizontally sharing UV (chroma or C) with a neighboring pixel and vertically sharing UV with the neighboring line (thus line 1 with line 2, line 3 with 4, etc).</p>
<p>The layout of a progressive 4:2:0 encoded image looks as follows (MPEG 2 scheme - see below)</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%">Y_Y_Y_Y</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%">C___C__</td>
    <td width="50%">&nbsp;</td>
  </tr>
  <tr>
    <td width="50%"> Y_Y_Y_Y</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%">&nbsp;</td>
    <td width="50%">&nbsp;</td>
  </tr>
  <tr>
    <td width="50%"> Y_Y_Y_Y</td>
    <td width="50%">line 3</td>
  </tr>
  <tr>
    <td width="50%"> C___C__</td>
    <td width="50%">&nbsp;</td>
  </tr>
  <tr>
    <td width="50%"> Y_Y_Y_Y</td>
    <td width="50%">line 4</td>
  </tr>
  <tr>
    <td width="50%">&nbsp;</td>
    <td width="50%">&nbsp;</td>
  </tr>
</table>
<p><b>YV12 interlaced</b></p>
<p>For each pixel, it is horizontally sharing UV (chroma or C) with a neighboring pixel and vertically sharing UV with the next to neighboring line (thus line 1t with line 3t, line 2b with 4b, etc).</p>
<p>The layout of a interlaced 4:2:0 encoded image looks as follows (MPEG 2 scheme - see below)</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%"> Y_Y_Y_Y</td>
    <td width="50%"> line 1t</td>
  </tr>
  <tr>
    <td width="50%"> C___C__</td>
    <td width="50%">&nbsp;</td>
  </tr>
  <tr>
    <td width="50%"> Y_Y_Y_Y</td>
    <td width="50%"> line 2b</td>
  </tr>
  <tr>
    <td width="50%">&nbsp;</td>
    <td width="50%">&nbsp;</td>
  </tr>
  <tr>
    <td width="50%"> Y_Y_Y_Y</td>
    <td width="50%">line 3t</td>
  </tr>
  <tr>
    <td width="50%"> C___C__</td>
    <td width="50%">&nbsp;</td>
  </tr>
  <tr>
    <td width="50%"> Y_Y_Y_Y</td>
    <td width="50%">line 4b</td>
  </tr>
  <tr>
    <td width="50%">&nbsp;</td>
    <td width="50%">&nbsp;</td>
  </tr>
</table>
<p>or</p>
<table border="1" width="50%">
  <tr>
    <td width="33%"> field 1</td>
    <td width="33%"> field 2</td>
    <td width="33%"> line</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">&nbsp;</td>
    <td width="33%"> line 1t</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 2b</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">line 3t</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">line 4b</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
</table>
<h3>2.2 Subsampling</h3>
<p>Subsampling is used to reduce the storage and broadcast bandwidth requirements for digital video. This is effective for a !YCbCr signal because the human eye is more sensitive for changes in black and white than for changes in color. So drastically reducing the color info shows very little difference. YUY2 and YV12 are examples of reduced color formats.</p>
<h4>RGB -> YUY2 conversion</h4>
<p>More about RGB -> YUV color conversions can be found here: <a href="color_conversions.htm">ColorConversions</a>.<br>
Recall the layout of a 4:4:4 encoded image</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%"> YC1 YC2 YC3 YC4</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%"> YC1 YC2 YC3 YC4</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%"> YC1 YC2 YC3 YC4</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%"> YC1 YC2 YC3 YC4</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<p>In AviSynth, the default mode is using a 1-2-1 kernel to interpolate chroma, that is</p>
<p>C1x = (C1+C1+C1+C2)/4 (C1 is used three times, since this is the border)<br>
C3x = (C2+C3+C3+C4)/4<br>
C5x = (C4+C5+C5+C6)/4</p>
<p>The 4:2:2 encoded image becomes</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%">Y1C1x Y2 Y3C3x Y4</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%">Y1C1x Y2 Y3C3x Y4</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%">Y1C1x Y2 Y3C3x Y4</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%">Y1C1x Y2 Y3C3x Y4</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<p>The other mode <a href="../corefilters/convert.htm">ConvertBackToYUY2</a> uses chroma from the left pixel, thus</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2 Y3C3 Y4</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2 Y3C3 Y4</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2 Y3C3 Y4</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2 Y3C3 Y4</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<p><i>Note (as with the layout of other formats) the position of the chroma values, represent the WEIGHT result of the
subsampling.</i></p>
<p><b>YUY2 -> YV12 interlaced conversion</b></p>
<p>Recall the layout of a interlaced 4:2:0 encoded image, but with the weights included:</p>
<table border="1" width="50%">
  <tr>
    <td width="33%">frame</td>
    <td width="33%"> line</td>
    <td width="33%">weights</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 1t</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%"> chroma of YUY2_lines<br>
      (0.75)*1t + (0.25)*3t</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 2b</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">line 3t</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%"> chroma of YUY2_lines<br>
      (0.25)*2b + (0.75)*4b</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">line 4b</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
</table>
<p>or</p>
<table border="1" width="50%">
  <tr>
    <td width="25%"> field 1</td>
    <td width="25%"> field 2</td>
    <td width="25%"> line</td>
    <td width="25%">weights</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%"> line 1t</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%"> chroma of YUY2_lines<br>
      (0.75)*1t + (0.25)*3t</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%"> line 2b</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">line 3t</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%"> chroma of YUY2_lines<br>
      (0.25)*2b + (0.75)*4b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">line 4b</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>
<p><i>Note (as with the layout of other formats) the position of the chroma values, represent the WEIGHT as a result of the
subsampling.</i></p>
<p>Thus the chroma is stretched across two luma lines in the same field!</p>
<p><b>YUY2 -> YV12 progressive conversion</b></p>
<p>Recall the layout of a 4:2:0 encoded image</p>
<table border="1" width="50%">
  <tr>
    <td width="33%">frame</td>
    <td width="33%"> line</td>
    <td width="33%">weights</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 1</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%"> chroma of YUY2_lines<br>
      (0.5)*1 + (0.5)*2</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 2</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">line 3</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%"> chroma of YUY2_lines<br>
      (0.5)*3 + (0.5)*4</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">line 4</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
</table>
<p><i>Note (as with the layout of other formats) the position of the chroma values, represent the WEIGHT result of the
subsampling.</i></p>
<p>Thus the chroma is stretched across two luma lines in the same frame!</p>
<h3>2.3 Upsampling</h3>
<h4>YUY2 conversion -> RGB</h4>
<p>Recall the layout of a 4:2:2 encoded image</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2 Y3C3 Y4</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2 Y3C3 Y4</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2 Y3C3 Y4</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2 Y3C3 Y4</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<p>The C++ and the ASM code use different sampling methods.</p>
<p>For the C++ code ConvertToRGB uses the same chroma for two RGB pixels using
left point upsampling. Thus the 4:4:4 encoded image becomes</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2C1 Y3C3 Y4C3</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2C1 Y3C3 Y4C3</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2C1 Y3C3 Y4C3</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2C1 Y3C3 Y4C3</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<p>For the ASM code ConvertToRGB the missing chroma samples are interpolated
(using a [1 1] kernel), that is</p>
C2x = (C1+C3)/2<br>
C4x = (C3+C5)/2
<p>and the existing chroma samples are just copied.</p>
<p>The 4:4:4 encoded image becomes</p>
<table border="1" width="50%">
  <tr>
    <td width="50%"> frame</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2C2x Y3C3 Y4C4x</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2C2x Y3C3 Y4C4x</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2C2x Y3C3 Y4C4x</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%">Y1C1 Y2C2x Y3C3 Y4C4x</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<p> The ASM code is the one
which is actually used in AviSynth.</p>
<h4>YV12 interlaced conversion -> YUY2</h4>
<p>In AviSynth, the missing chroma samples are interpolated as follows</p>
<table border="1" width="50%">
  <tr>
    <td width="33%">frame</td>
    <td width="33%"> line</td>
    <td width="33%">weights</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 1t</td>
    <td width="33%"> chroma of YV12_lines<br>
      1t</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 2b</td>
    <td width="33%"> chroma of YV12_lines<br>
      4b</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">line 3t</td>
    <td width="33%"> chroma of YV12_lines<br>
      (0.75)*1t + (0.25)*5t</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%">line 4b</td>
    <td width="33%"> chroma of YV12_lines<br>
      (0.75)*4b + (0.25)*8b</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 5t</td>
    <td width="33%"> chroma of YV12_lines<br>
      (0.25)*1t + (0.75)*5t</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 6b</td>
    <td width="33%"> chroma of YV12_lines<br>
      (0.25)*4b + (0.75)*8b</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 7t</td>
    <td width="33%"> chroma of YV12_lines<br>
      (0.75)*5t + (0.25)*9t</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 8b</td>
    <td width="33%"> chroma of YV12_lines<br>
      (0.75)*8b + (0.25)*12b</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
  </tr>
</table>
<p>or</p>
<table border="1" width="50%">
  <tr>
    <td width="25%"> field 1</td>
    <td width="25%"> field 2</td>
    <td width="25%"> line</td>
    <td width="25%">weights</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%"> line 1t</td>
    <td width="25%"> chroma of YV12_lines<br>
      1t</td>
  </tr>
  <tr>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%"> line 2b</td>
    <td width="25%"> chroma of YV12_lines<br>
      4b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">line 3t</td>
    <td width="25%"> chroma of YV12_lines<br>
      (0.75)*1t + (0.25)*5t</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">line 4b</td>
    <td width="25%"> chroma of YV12_lines<br>
      (0.75)*4b + (0.25)*8b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">line 5t</td>
    <td width="25%"> chroma of YV12_lines<br>
      (0.25)*1t + (0.75)*5t</td>
  </tr>
  <tr>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">line 6b</td>
    <td width="25%"> chroma of YV12_lines<br>
      (0.25)*4b + (0.75)*8b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">line 7t</td>
    <td width="25%"> chroma of YV12_lines<br>
      (0.75)*5t + (0.25)*9t</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">line 8b</td>
    <td width="25%"> chroma of YV12_lines<br>
      (0.75)*8b + (0.25)*12b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>
<p>This implementation results in a <a href="http://forum.doom9.org/showthread.php?p=1294886#post1294886">chroma
shift</a>. AviSynth uses a different interpolation as the one suggested by the mpeg2 specs (perhaps due to speed issues). The latter is</p>
<table border="1" width="50%">
  <tr>
    <td width="25%"> field 1</td>
    <td width="25%"> field 2</td>
    <td width="25%"> line</td>
    <td width="25%">weights</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%"> line 1t</td>
    <td width="25%"> chroma of YV12_lines<br>
      1t</td>
  </tr>
  <tr>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%"> line 2b</td>
    <td width="25%"> chroma of YV12_lines<br>
      4b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">line 3t</td>
    <td width="25%"> chroma of YV12_lines<br>
      (5/8)*1t + (3/8)*5t</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">line 4b</td>
    <td width="25%"> chroma of YV12_lines<br>
      (7/8)*4b + (1/8)*8b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">line 5t</td>
    <td width="25%"> chroma of YV12_lines<br>
      (1/8)*1t + (7/8)*5t</td>
  </tr>
  <tr>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">line 6b</td>
    <td width="25%"> chroma of YV12_lines<br>
      (3/8)*4b + (5/8)*8b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">line 7t</td>
    <td width="25%"> chroma of YV12_lines<br>
      (5/8)*5t + (3/8)*9t</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> C___C__</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%"> Y_Y_Y_Y</td>
    <td width="25%">line 8b</td>
    <td width="25%"> chroma of YV12_lines<br>
      (7/8)*8b + (1/8)*12b</td>
  </tr>
  <tr>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>
<p><b>YV12 progressive conversion -> YUY2</b></p>
<p>The missing chroma samples are interpolated as follows</p>
<table border="1" width="50%">
  <tr>
    <td width="33%">frame</td>
    <td width="33%"> line</td>
    <td width="34%">weights</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 1</td>
    <td width="34%"> chroma of YV12_lines<br>
 1</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 2</td>
    <td width="34%"> chroma of YV12_lines<br>
 (0.75)*1 + (0.25)*3</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 3</td>
    <td width="34%"> chroma of YV12_lines<br>
 (0.25)*1 + (0.75)*3</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 4</td>
    <td width="34%"> chroma of YV12_lines<br>
 (0.75)*3 + (0.25)*5</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 5</td>
    <td width="34%"> chroma of YV12_lines<br>
 (0.25)*3 + (0.75)*5</td>
  </tr>
  <tr>
    <td width="33%"> C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 6</td>
    <td width="34%"> chroma of YV12_lines<br>
 (0.75)*5 + (0.25)*7</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
</table>
<h3>2.4 References</h3>
<p><font color="#FF0000">ColorSpaces</font><br>
[<a href="http://www.quantel.com/domisphere/infopool.nsf/HTML/dfb444?OpenDocument">4:4:4</a>] sampling<br>
[<a href="http://www.quantel.com/domisphere/infopool.nsf/HTML/dfb422?OpenDocument">4:2:2</a>] sampling<br>
[<a href="http://www.quantel.com/domisphere/infopool.nsf/HTML/dfb420?OpenDocument">4:2:0</a>] sampling<br>
[<a href="http://www.hometheaterhifi.com/volume_8_2/dvd-benchmark-special-report-chroma-bug-4-2001.html">Chroma
Upsampling</a>]<br>
[<a href="http://www.mir.com/DMG/chroma.html">Chroma Subsampling Standards</a>]</p>
<h3>3.1 MPEG-1 versus MPEG-2 sampling</h3>
<p>There are two common variants of 4:2:0 sampling. One of these is used in MPEG-2 (and CCIR-601) video, and the other is used in MPEG-1.
<b>The MPEG-2 scheme is how AviSynth samples 4:2:0 video</b>, because it completely avoids horizontal resampling in
4:2:0 &lt;-> 4:2:2 conversions.</p>
<p>The layout of a progressive MPEG-1 4:2:0 encoded image</p>
<table border="1" width="50%">
  <tr>
    <td width="33%">frame</td>
    <td width="33%"> line</td>
    <td width="34%">weights</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 1</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">_C__C_</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">chroma of YUY2_lines<br>
      (0.5)*1 + (0.5)*2</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 2</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 3</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">_C__C_</td>
    <td width="33%">&nbsp;</td>
    <td width="34%"> chroma of YUY2_lines<br>
      (0.5)*3 + (0.5)*4</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 4</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
</table>
<p>The layout of a MPEG-2 4:2:0 encoded image</p>
<table border="1" width="50%">
  <tr>
    <td width="33%">frame</td>
    <td width="33%"> line</td>
    <td width="34%">weights</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 1</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="34%"> chroma of YUY2_lines<br>
      (0.5)*1 + (0.5)*2</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 2</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 3</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">C___C__</td>
    <td width="33%">&nbsp;</td>
    <td width="34%"> chroma of YUY2_lines<br>
      (0.5)*3 + (0.5)*4</td>
  </tr>
  <tr>
    <td width="33%"> Y_Y_Y_Y</td>
    <td width="33%"> line 4</td>
    <td width="34%">&nbsp;</td>
  </tr>
  <tr>
    <td width="33%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="34%">&nbsp;</td>
  </tr>
</table>
<h3>3.2 DV sampling</h3>
<p>For completeness, we will mention DV sampling. DV is 4:2:0 (PAL) and 4:1:1 (NTSC). Note, that the sample positioning of the former is different from the
4:2:0 chroma in MPEG-1/MPEG-2!</p>
<p>The layout of a 4:2:0 encoded image (field-based)</p>
<table border="1" width="50%">
  <tr>
    <td width="50%">field</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%"> YV Y YV Y YV Y YV Y</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%"> YU Y YU Y YU Y YU Y</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%"> YV Y YV Y YV Y YV Y</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%"> YU Y YU Y YU Y YU Y</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<p>The layout of a 4:1:1 encoded image (field-based)</p>
<table border="1" width="50%">
  <tr>
    <td width="50%">field</td>
    <td width="50%"> line</td>
  </tr>
  <tr>
    <td width="50%"> YC Y Y Y YC Y Y Y&nbsp;</td>
    <td width="50%"> line 1</td>
  </tr>
  <tr>
    <td width="50%"> YC Y Y Y YC Y Y Y&nbsp;</td>
    <td width="50%"> line 2</td>
  </tr>
  <tr>
    <td width="50%"> YC Y Y Y YC Y Y Y&nbsp;</td>
    <td width="50%"> line 3</td>
  </tr>
  <tr>
    <td width="50%"> YC Y Y Y YC Y Y Y&nbsp;</td>
    <td width="50%"> line 4</td>
  </tr>
</table>
<p>Some comments about this formats:<br>
- 4:1:1 is supported natively in AviSynth v2.6.<br>
- DV decoders all output YUY2 or RGB (with the exception of ffdshow when YV12 is enabled).<br>
- When outputting YUY2/RGB (NTSC), the MainConcept codec duplicates the chroma samples instead of interpolating. The
[<a href="http://mywebpages.comcast.net/trbarry/downloads.htm">ReInterpolate411
plugin</a>] can be used to correct for this, resulting in better quality.</p>
<h3>3.3 References</h3>
<p>[<a href="http://http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/YUVFormats.asp">MSDN:
YUV sampling</a>] Describes the most common YUV sampling techniques.<br>
[<a href="http://www.adamwilt.com/pix-sampling.html">DV sampling</a>]</p>
<h2>4. 4:2:0 Interlaced Chroma Problem (or ICP)</h2>
<p>In general interlaced content will have static parts. If it is upsampled
correctly using interlaced upsampling, it will still have <i>chroma problems on
diagonal edges of bright-colored objects in static parts of a frame</i>. The
reason is that &quot;When the two fields are put back together later by a deinterlacer (or by your eye and brain, if you watch it on an interlaced TV), the relatively smooth gradations and contours of each field are broken up by a slightly different set of gradations and contours from the other field.&quot; (quote from first reference). This is called
<b>the Interlaced Chroma Problem</b>.
The &quot;solution&quot; is a motion-adaptive upsampler, but such an
AviSynth/VDub filter which attempts to do this doesn't exist yet.</p>
<p>References:<br>
[<a href="http://www.hometheaterhifi.com/volume_8_2/dvd-benchmark-special-report-chroma-bug-4-2001.html">The
4:2:0 Interlaced Chroma Problem</a>]<br>
[<a href="http://members.aol.com/ajaynejr/vidbug2.htm">The 4:2:0 Interlaced
Chroma Problem - Television and Video Advice</a>]</p>
<p><kbd>$Date: 2010/06/05 14:47:46 $</kbd></p>
<FORM><INPUT TYPE="Button" VALUE="Back"
onClick="history.go(-1)"></FORM>
</BODY>
</HTML>
